package com.top.horizon.th1804app.adapter;

import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;

public class FramesItemTouchHelperCallback extends ItemTouchHelper.Callback {
    private MoreFramesAdapter mAdapter;

    public FramesItemTouchHelperCallback(MoreFramesAdapter myAdapter) {
        this.mAdapter = myAdapter;
    }

    //决定拖拽/滑动的方向
    @Override
    public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        //int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN; //允许上下的拖动
        //int dragFlags =ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT; //允许左右的拖动
        //int swipeFlags = ItemTouchHelper.LEFT; //只允许从右向左侧滑
        //int swipeFlags = ItemTouchHelper.DOWN; //只允许从上向下侧滑
        //一般使用makeMovementFlags(int,int)或makeFlag(int, int)来构造我们的返回值
//        makeMovementFlags(dragFlags, swipeFlags)

        int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN | ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT; //允许上下左右的拖动
        return makeMovementFlags(dragFlags, 0);
//        return 0;
    }

    //和位置交换有关,可用于实现drag功能
    @Override
    public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
        //通过接口传递拖拽交换数据的起始位置和目标位置的ViewHolder
        mAdapter.onItemMove(viewHolder, target);
        return true;
    }

    //和滑动有关,可用于实现swipe功能
    @Override
    public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {

    }


    //是否长按启用拖拽功能,默认是true
    public boolean isLongPressDragEnabled() {
        return true;
    }

    //是否支持滑动,默认true
    public boolean isItemViewSwipeEnabled() {
        return true;
    }

    //和目标View的状态改变有关,例如drag,swipe,ide
    public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {

    }

    //和移除View的状态有关,通常用于清除在onSelectedChanged,onChildDraw中对View设置的动画
    public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        if (!recyclerView.isComputingLayout()) {
            //当需要清除之前在onSelectedChanged或者onChildDraw,onChildDrawOver设置的状态或者动画时通过接口返回该ViewHolder
            mAdapter.onItemClear(viewHolder);
        }
    }
}
